package com.gopay.bis.payease.ebank.crypt;

import java.io.FileInputStream;
import java.io.IOException;

import org.bouncycastle.util.encoders.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.capinfo.crypt.RSA_MD5;

public class GoPayRsaMd5 {
	private final static Logger logger = LoggerFactory.getLogger(GoPayRsaMd5.class);
	
	private String pubkeyPath;
	private String publicKey = null;
	
	public int publicVerifyMD5(String MD5Value, String src) {
	    String mod = RSA_MD5.GetValue("m=", publicKey);
	    String pubExp = RSA_MD5.GetValue("e=", publicKey);

	    byte[] b = Hex.decode(MD5Value);
	    RSA_MD5 rsaMd5 = new RSA_MD5();
	    b = rsaMd5.PublicKeyDecrypt(mod, pubExp, b);

	    String key = new String(b);
	    String ss = RSA_MD5.GetMD5(src.getBytes());

	    return key.compareToIgnoreCase(ss);
	}
	
	public void setPubkeyPath(String pubkeyPath) {
		this.pubkeyPath = pubkeyPath;
		FileInputStream f = null;
	    try
	    {
	    	logger.info("payease public key path = " + pubkeyPath);
	        f = new FileInputStream(pubkeyPath);
	        byte[] b = new byte[f.available()];
	        f.read(b);
	        this.publicKey = new String(b);
	        logger.info("payease public key = " + publicKey);
	    }
	    catch (Exception e)
	    {
	    	logger.error("load payease public key error !!!", e);
	    }finally{
	    	if(f != null)
				try {
					f.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	    }
	}
}
